from rmasm.translaters.macro_move_command import *

# ======================
#   赋值语法糖命令转换器
# ======================
# key 是 宏的名字
# 参数是 变量名 和 宏
flattenable_macro_move_command_translators: Dict[str, Callable[[str, Macro], str]] = {}


def flattenable_macro_move_command_translator(name):
    def wrapper(func):
        flattenable_macro_move_command_translators[name] = func
        return func

    return wrapper


# ================ 程序基础 ================

@flattenable_macro_move_command_translator('pop')
def f_mmct_pop(var: str, macro: Macro) -> str:
    yield f'{var} = ss[sp]'
    yield f'sp = sp - 1'


@flattenable_macro_move_command_translator('peek')
def f_mmct_peek(var: str, macro: Macro) -> str:
    yield f'{var} = ss[sp]'


# ================ 快捷语法 ================

# 管道运算
@flattenable_macro_move_command_translator('pipe')
def f_mmct_calc(var: str, macro: Macro) -> str:
    s = macro.args[0].split()
    yield f'__temp = {s[0]}'
    for i in range(1, len(s), 2):
        yield f'__temp = __temp {s[i]} {s[i + 1]}'
    yield f"{var} = __temp"
